﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module;
using System.Collections.Generic;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using Cit.CDE.Net.Service;
using System.Text;

using Module;


/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        AddSys.aspx.cs           
/// Author :         王迪
/// CDT:             2011-12-13           
/// Version:         1.0.0.0                
/// Depiction:       系统信息修改    
/// </summary>
public partial class main_admin_SysEdit : UserControl.CommonPage
{
    /// <summary>
    /// 系统信息EM
    /// </summary>
    private CDEEntityManager em;
    /// <summary>
    /// 系统信息Entity
    /// </summary>
    private CDEEntity m_entity;
    /// <summary>
    /// 系统信息Entity名字
    /// </summary>    
    private const string EM_FULL_NAME = WEBConstants.EM_SYS_MANAGE;

    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //系统信息ID
        string chkID = "1";
        //系统CODE不作处理
        tr_SYS_CODE.Controls.Clear();

        em = CDEEMFactory.getEMByFullName(EM_FULL_NAME);
        if (!Page.IsPostBack)
        {
            //ID不为空修改系统信息，否则新增系统信息
            if (chkID != "" && chkID != null)
            {
                //修改
                this.page_title.InnerText = "系统信息修改";
                CDESearchCriteria sc = new CDESearchCriteria(em);
                sc.addCondition("ID", chkID);
                List<CDEEntity> listEntity = CDEService.LoadEntities(em, sc);
                //系统信息ID是否存在
                if (listEntity.Count > 0)
                {
                    m_entity = listEntity[0];
                    m_entity.Operation = CDEDataOperation.UPDATE;
                }
                else
                {
                    m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
                    m_entity.Operation = CDEDataOperation.INSERT;
                }
            }
           
            Session["MENTITY"] = m_entity;
            //系统信息信息设值到页面
            this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);
        }
        else
        {
            m_entity = (CDEEntity)Session["MENTITY"];
            //页面信息放入系统信息Entity
            this.setEntityFromRequest(m_entity, Request.Params);
        }
    }
    /// <summary>
    /// 确定按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void queding_Click(object sender, EventArgs e)
    {
        List<CDEEntity> listEntity = new List<CDEEntity>();

        listEntity.Add(m_entity);

        bool isSaveError = false;

        //系统代码初始设置
        //string guid = Guid.NewGuid().ToString().ToUpper();
        //m_entity.set("SYS_CODE",guid);
             
        CDESearchCriteria vsc = new CDESearchCriteria(em);
        CDEService.UpdateEntities(em, listEntity);

        if (listEntity[0].Result == CDEDataOperationResult.ERROR)
        {
            isSaveError = true;
        }
        else
        {
            isSaveError = false;
        }

        //成功返回列表，失败提示错误信息
        if (isSaveError)
        {

            showMessageErrAndOk(Resources.ResourceErrMsg.SAVE_ERR);
            this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);
        }
        else
        {
            Sys_Info sysInfo = new Sys_Info(m_entity);
            //系统信息存入Session
            Session[WEBConstants.SYS_INFO_SESSION_ID] = sysInfo;

            StringBuilder sbr = new StringBuilder();
            sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
            sbr.AppendFormat("alert(\"");
            sbr.AppendFormat(Resources.Resource.RESULT_OK);
            sbr.AppendFormat("\");</script>");
            //sbr.AppendFormat("\");window.location='../admin/SysEdit.aspx?EM=" + EM_FULL_NAME + "&sel_menu_id=" + Sel_MenuNode.Attributes["id"].Value + "&IsBackSc=1';</script>{0}", Environment.NewLine);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sbr.ToString());
        }

    }
 
}
